package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.smarthome.common.mobile.Service;
import com.smarthome.core.advicemanager.SystemMessageService;
import com.smarthome.domain.bean.ShAccountMessage;
import com.smarthome.domain.enums.AccountMessageReceiveStatus;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileRequest;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobileAccountMessageRequest;

@org.springframework.stereotype.Service("mobile.accountMessage.service")
public class MobileAccountMessageService implements Service<MobileResponse<?>> {
	
	@Resource
	private SystemMessageService systemMessageService;
	
	/**
	 * 
	* getList
	* @Title: getList
	* @Description: 获取用户消息列表
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月15日 下午5:00:03
	 */
	public void getList(MobileAccountMessageRequest request,MobileResponse<List<ShAccountMessage>> response) {
		if(request.getAccountId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_MESSAGE_NOT_RECIEVERID);
			return;
		}
		
		ShAccountMessage queryAccountMessage = new ShAccountMessage();
		BeanUtils.copyProperties(request, queryAccountMessage);
		queryAccountMessage.setRecieverId(request.getAccountId());
		queryAccountMessage.setOrderField("sendLastTime");
		queryAccountMessage.setOrderFieldType("desc");
		int count = systemMessageService.count(queryAccountMessage);
		if(request.getStartIndex() != null && request.getStartIndex() >= count) {
			//response.setMobileCodeObj(MobileCodeEnum.PAGE_NOTSHOW);
			response.setData(new ArrayList<ShAccountMessage>());
			return;
		}
		try {
			List<ShAccountMessage> accountMessages = systemMessageService.getAccountMessages(queryAccountMessage);
			List<Long> ids = new ArrayList<Long>(); 
			int length = accountMessages.size();
			for(int i = 0;i < length;i++) {
				if(accountMessages.get(i).getRecieveStatus() != null && !accountMessages.get(i).getRecieveStatus().equals(AccountMessageReceiveStatus.VIEW.getCode())) {
					accountMessages.get(i).setRecieveStatus(AccountMessageReceiveStatus.VIEW.getCode());
					ids.add(accountMessages.get(i).getId());
				}
			}
			if(ids.size() != 0) {
				ShAccountMessage updateAccountMessage = new ShAccountMessage();
				updateAccountMessage.setRecieveStatus(AccountMessageReceiveStatus.VIEW.getCode());
				updateAccountMessage.setRecieveLastTime(new Date());
				updateAccountMessage.addQueryData("ids", ids);
				systemMessageService.batchUpdate(updateAccountMessage);
			}
			response.setData(accountMessages);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
		
	}
	

	
	/**
	 * 
	* delete
	* @Title: delete
	* @Description: 删除系统消息
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月15日 下午9:26:07
	 */
	public void delete(MobileAccountMessageRequest request,MobileResponse<?> response) {
		if(request.getId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_MESSAGE_NOT_ID);
			return;
		}
		int result = 0;
		try {
			result = systemMessageService.delete(request.getId());
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
		if(result != 1) {
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_MESSAGE_DEL_FAIL);
		}
	}
	
	/**
	 * 
	* getNotViewedCount
	* @Title: getNotViewedCount
	* @Description: 获取用户未读的消息数目
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年11月18日 上午11:09:45
	 */
	public void getNotViewedCount(MobileRequest request,MobileResponse<Integer> response) {
		if(request.getAccountId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_MESSAGE_NOT_RECIEVERID);
			return;
		}
		
		ShAccountMessage queryAccountMessage = new ShAccountMessage();
		queryAccountMessage.setRecieverId(request.getAccountId());
		queryAccountMessage.setRecieveStatus(AccountMessageReceiveStatus.NOTVIEWED.getCode());
		
		try {
			int count = systemMessageService.count(queryAccountMessage);
			response.setData(count);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
	}

}
